'''oto/site.py

This module contains the Site model for oto.
'''

__author__ = 'Ryan Rousseau <ryan.rousseau@gmail.com>'
__license__ = '''Oranges to Oranges
Copyright (C) 2006  Ryan Rousseau

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
'''

import os
import shelve

from util import *

__all__ = ['Site']

class Site(object):
    '''Site([name, host])

This model represents an oto server site.
name is the name used to identify the site.
host is the hostname or ip address of the site.
'''
    def __init__(self, name='', host=''):
        self.name = name
        self.host = host

    def __repr__(self):
        return str(self)

    def __str__(self):
        return '<Site "%s": %s>' % (self.name, self.host)

    def save(self, name=None):
        if name:
            self.name = name

        if not self.name:
            return

        filename = os.path.join(get_site_dir(), '%s.site' % self.name)
        f = shelve.open(filename)
        f['name'] = self.name
        f['host'] = self.host
        f.close()

    def load(self, name=None):
        if name:
            self.name = name

        if not self.name:
            return

        filename = os.path.join(get_site_dir(), '%s.site' % self.name)
        f = shelve.open(filename)
        self.name = f['name'] 
        self.host = f['host']
        f.close()
